<template>
	<!-- bgcolor="#FD9031" -->
	<view class="cinemamain" @touchmover.stop="">
		<!-- <title :center="false" @titleHeight='setPaddingTop' title="我的订单" iconSize="38" bgcolor="linear-gradient(45deg, #f43f3b, #ec008c)"    color="#FFF"
		 @toclick="toBack(1)"icon="/static/img/other/to-back.png"></title> -->
		 
		 <title @titleHeight='setPaddingTop' :center="true" title="电影订单" bgcolor="white" iconSize="38"
		 	@toclick="toBack(1)"icon='icon'>
		 </title>
		 
		<view class="movie_top " :style="{'padding-top': Number(height) + 'px'}" ></view>
		<!-- 订单列表盒子 start -->
		<scroll-view scroll-y class="p20" :style="{height: boxHeight + 'px'}" :refresher-enabled="triggered"
		 :refresher-triggered="freshing" refresher-enabled @refresherrefresh="refresh(true)" @scrolltolower="onreachBottom">
			<order-item v-show="loading" :list="orderList" @loadingFuc="loadingFuc"></order-item>
		</scroll-view>
		
		<!-- 订单列表盒子 end -->
	</view>
</template>

<script>
	import title from '@/pages/components/title.vue'
	import orderItem from '../components/order-Item.vue'

	export default {
		data() {
			return {
				loading:true,
				boxHeight: 0,
				// 下拉刷新
				triggered: false,
				freshing: false,
				height: 0,
				loaded: false,
				
				condition: {
					page: 0,
					pageSize: 20
				},
				orderList: [],
				is_coupon:false,
			}
		},
		components: {
			title,
			orderItem
		},
		onLoad(option) {
			// console.log(option.type);
			switch (option.type){
				case 'total':
					this.is_coupon = false
					break;
				case 'coupon':
					this.is_coupon = true
					break;
				
			}
		},
		onShow() {
			uni.showLoading({
				title: '加载中'
			})
			this.refresh()
		},
		methods: {
			setPaddingTop(data){
				this.height = data.height
				this.boxHeight = (uni.getSystemInfoSync().windowHeight - this.height)
			},
			
			// 返回
			toBack(index) {
				let pages = getCurrentPages()
				let prePages = pages[pages.length - 2 ].route || ''
				
				if(/order-detail/.test(prePages)){
					uni.reLaunch({
						url:'/pages/first/first'
					})
				}else{
					this.$common.toBack(index)
				}
			},
			// 时间计算
			countDown(createTime, logtime) {
				let resulttime = ''
				const time = new Date(createTime).getTime() // 创建订单时间戳
				const logtimeF = time + logtime * 10 * 60 * 100 // 增加时间后的时间戳
				const nowtime = new Date().getTime() // 当前时间戳
				resulttime = logtimeF - nowtime
				if (resulttime < 0) {
					resulttime = 0
					return resulttime
				}
				return resulttime / 1000
			},
			// 订单信息
			getMyOrder() {
				this.loading = false
				this.$api.getOrder({
					limit: this.condition.pageSize,
					offset: this.condition.page * this.condition.pageSize,
					is_coupon: this.is_coupon?'4':null,//1:使用优惠券；2：使用电影卡；3：普通订单，优惠券电影卡都不使用；4：除了普通订单的其余全部
				}).then(res => {
					this.loading = true
					let dataInfo = res.data.data.rows
					dataInfo.forEach(item => {
						item.m0 = this.countDown(item.created_at, 10)
						if (item.m0 === 0 && item.status === 0 && item.is_pay === 0) {
							this.$api.releaseOrder({
								oid: item.id
							}).then(relea => {
								this.refresh()
							})
						}
					})
					this.orderList.push(...res.data.data.rows)
					this.condition.page += 1
					if (res.data.data.count <= this.condition.page * this.condition.pageSize) {
						this.loaded = true
					}
					this.freshing = false
					uni.hideLoading()
				})

			},
			loadingFuc() {
				this.refresh()
			},
			// 下拉刷新事件
			refresh(freshing = null) {
				if (freshing) {
					this.freshing = true
				}
				this.loaded = false
				this.condition.page = 0
				this.orderList = []
				uni.showLoading({
					title: '加载中'
				})
				this.getMyOrder()
			},
			//触底事件
			onreachBottom() {
				if (!this.loaded) {
					uni.showLoading({
						title: '加载中'
					})
					this.getMyOrder()
				}
			},
		},
	}
</script>
<style lang="scss" scoped>
	.cinemamain {
		background-color: #F4F4F4;
		font-size: 28rpx;
		color: #323232;
		height: 100vh;
		overflow: hidden;
		
	}
</style>
